package com.edu.compile.compiler;

import com.edu.compile.AbstractCompileResult;
import com.edu.compile.AbstractCompilerParser;
import com.edu.compile.config.HttpResponseConfig;
import com.edu.compile.metadata.SourceMetadata;
import com.edu.compile.template.ftl.node.HttpResponse;
import com.edu.model.dto.NodeMetadata;
import org.springframework.stereotype.Component;

@Component("http_response_compiler")
public class HttpResponseCompiler extends AbstractCompilerParser<HttpResponseConfig, HttpResponse> {

    @Override
    public void compile(AbstractCompileResult abstractCompileResult, NodeMetadata nodeMetadata, HttpResponseConfig httpResponseConfig, HttpResponse httpResponse) {

        SourceMetadata sourceMetadata = httpResponseConfig.getSourceMetadata();
        Object obj = sourceMetadata.getValue();
        httpResponse.setInput(obj);

//        boolean loop = abstractCompileResult.isLoop();
//        CompileResult compileResult = (CompileResult) abstractCompileResult;
//        HttpR httpR = compileResult.getEventContainer().getNodeTemplateModel(httpResponseNodeDto.getNodeId());
//        httpR.setLoop(loop);
//        HttpResponseParam httpResponseParam = httpResponseNodeDto.getHttpResponseParam();
//        Object obj = null;
//        String value = httpResponseParam.getValue();
//        String methodPrefix = getMethodPrefix(eventId);
//        if (httpResponseParam.getSource() == EventConstant.SourceConstant.FRO) {
//            if (value.contains(EventConstant.ID_SEPARATOR_VAL)) {
//                String[] params = value.split(EventConstant.ID_SEPARATOR_VAL);
//                obj = "storage['" + methodPrefix + params[0].trim() + "']." + params[1].trim();
//            } else {
//                obj = "storage['" + methodPrefix + value + "']";
//            }
//        } else {
//            obj = EventConstant.ParamType.wrapper(httpResponseParam.getSourceType(), value);
//            if (obj != null && httpResponseParam.getSourceType() == EventConstant.ParamType.STRUCT) {
//                String input = obj.toString();
//                Matcher matcher = EventConstant.Patterns.httpRVarPattern.matcher(input);
//                while (matcher.find()) {
//                    String group = matcher.group();
//                    String r1 = group.replace("\"", "");
//                    String r2 = findValueFromStorage(r1, methodPrefix);
//                    input = input.replace(group, r2);
//                }
//                obj = input;
//            }
//        }
//
//        httpR.setInput(obj);
    }
}
